package com.crnn.smart.utils;

import com.crnn.smart.entity.User;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class CurrentUserUtils {

    public User getCurrentUser(){
        User user = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof AnonymousAuthenticationToken) {
            user = null;
        }else if (authentication instanceof UsernamePasswordAuthenticationToken) {
            user =  (User) authentication.getPrincipal();
        }
        return user;
    }
}
